/*
 * lishanwu 2024.09.22
 */

/******************************************************************************
* boot2 binary code, this code will init XIP, set msp and jump to reset handler.
* boot2 must be at 0x10000000, size of 256 bytes.
*
* Winbond W25Q080
* Also supports W25Q16JV (which has some different SR instructions)
* Also supports AT25SF081
* Also supports S25FL132K0
*******************************************************************************/
.cpu cortex-m0plus
.thumb

.section .boot2, "ax"

.byte 0x00, 0xb5, 0x32, 0x4b, 0x21, 0x20, 0x58, 0x60, 0x98, 0x68, 0x02, 0x21, 0x88, 0x43, 0x98, 0x60
.byte 0xd8, 0x60, 0x18, 0x61, 0x58, 0x61, 0x2e, 0x4b, 0x00, 0x21, 0x99, 0x60, 0x02, 0x21, 0x59, 0x61
.byte 0x01, 0x21, 0xf0, 0x22, 0x99, 0x50, 0x2b, 0x49, 0x19, 0x60, 0x01, 0x21, 0x99, 0x60, 0x35, 0x20
.byte 0x00, 0xf0, 0x44, 0xf8, 0x02, 0x22, 0x90, 0x42, 0x14, 0xd0, 0x06, 0x21, 0x19, 0x66, 0x00, 0xf0
.byte 0x34, 0xf8, 0x19, 0x6e, 0x01, 0x21, 0x19, 0x66, 0x00, 0x20, 0x18, 0x66, 0x1a, 0x66, 0x00, 0xf0
.byte 0x2c, 0xf8, 0x19, 0x6e, 0x19, 0x6e, 0x19, 0x6e, 0x05, 0x20, 0x00, 0xf0, 0x2f, 0xf8, 0x01, 0x21
.byte 0x08, 0x42, 0xf9, 0xd1, 0x00, 0x21, 0x99, 0x60, 0x1b, 0x49, 0x19, 0x60, 0x00, 0x21, 0x59, 0x60
.byte 0x1a, 0x49, 0x1b, 0x48, 0x01, 0x60, 0x01, 0x21, 0x99, 0x60, 0xeb, 0x21, 0x19, 0x66, 0xa0, 0x21
.byte 0x19, 0x66, 0x00, 0xf0, 0x12, 0xf8, 0x00, 0x21, 0x99, 0x60, 0x16, 0x49, 0x14, 0x48, 0x01, 0x60
.byte 0x01, 0x21, 0x99, 0x60, 0x01, 0xbc, 0x00, 0x28, 0x00, 0xd0, 0x00, 0x47, 0x12, 0x48, 0x13, 0x49
.byte 0x08, 0x60, 0x03, 0xc8, 0x80, 0xf3, 0x08, 0x88, 0x08, 0x47, 0x03, 0xb5, 0x99, 0x6a, 0x04, 0x20
.byte 0x01, 0x42, 0xfb, 0xd0, 0x01, 0x20, 0x01, 0x42, 0xf8, 0xd1, 0x03, 0xbd, 0x02, 0xb5, 0x18, 0x66
.byte 0x18, 0x66, 0xff, 0xf7, 0xf2, 0xff, 0x18, 0x6e, 0x18, 0x6e, 0x02, 0xbd, 0x00, 0x00, 0x02, 0x40
.byte 0x00, 0x00, 0x00, 0x18, 0x00, 0x00, 0x07, 0x00, 0x00, 0x03, 0x5f, 0x00, 0x21, 0x22, 0x00, 0x00
.byte 0xf4, 0x00, 0x00, 0x18, 0x22, 0x20, 0x00, 0xa0, 0x00, 0x01, 0x00, 0x10, 0x08, 0xed, 0x00, 0xe0
.byte 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x74, 0xb2, 0x4e, 0x7a

/******************************************************************************
* reset handler.
*******************************************************************************/
.extern sys_clock_init
.extern eb_system_init
.extern main


.section  .text.reset_handler
.weak  reset_handler
.type  reset_handler, %function
reset_handler:
	/* Copy the data segment initializers from flash to SRAM */  
	ldr r0, =__data_vma_start
	ldr r1, =__data_vma_end
	ldr r2, =__data_lma_start
	movs r3, #0
	b loop_copy_data_init

copy_data_init:
	ldr r4, [r2, r3]
	str r4, [r0, r3]
	add r3, r3, #4

loop_copy_data_init:
	add r4, r0, r3
	cmp r4, r1
	bcc copy_data_init

	/* Zero fill the bss segment. */
	ldr r2, =__bss_start
	ldr r4, =__bss_end
	movs r3, #0
	b loop_fill_zero_bss

fill_zero_bss:
	str  r3, [r2]
	add r2, r2, #4

loop_fill_zero_bss:
	cmp r2, r4
	bcc fill_zero_bss

/* Call the clock system initialization function.*/
  	bl eb_machine_init
	bl eb_system_init
	bl main
	b .

/*******************************************************************************
* Provide weak aliases for each Exception handler to the Default_Handler. 
* As they are weak aliases, any function with the same name will override 
* this definition.
*******************************************************************************/
.global vectors

.section  .text.default_handler
.weak  default_handler
.type  default_handler, %function
default_handler:
	b default_handler

.section  .text.reserved_handler
.weak  reserved_handler
.type  reserved_handler, %function
reserved_handler:
	b reserved_handler

.section .vectors,"a",%progbits
.type vectors, %object
vectors:
	.word _stack
	.word reset_handler
	.word nmi_hadler
	.word hardfault_handler
	.word reserved_handler
	.word reserved_handler
	.word reserved_handler
	.word reserved_handler
	.word reserved_handler
	.word reserved_handler
	.word reserved_handler
	.word svc_handler
	.word reserved_handler
	.word reserved_handler
	.word pendsv_handler
	.word systick_handler
	.word timer_handler0
	.word timer_handler1
	.word timer_handler2
	.word timer_handler3
	.word pwm_handler
	.word usb_ctrl_handler
	.word xip_handler
	.word pio0_handler0
	.word pio0_handler1
	.word pio1_handler0
	.word pio1_handler1
	.word dma_handler0
	.word dma_handler1
	.word io_bank0_handler
	.word io_qspi_handler
	.word sio_proc0_handler
	.word sio_proc1_handler
	.word clocks_handler
	.word spi0_handler
	.word spi1_handler
	.word uart0_handler
	.word uart1_handler
	.word adc_fifo_handler
	.word i2c0_handler
	.word i2c1_handler
	.word rtc_handler
.size vectors, .-vectors

	.weak      nmi_hadler
	.thumb_set nmi_hadler, default_handler
	.weak      hardfault_handler
	.thumb_set hardfault_handler, default_handler
	.weak      svc_handler
	.thumb_set svc_handler, default_handler
	.weak      pendsv_handler
	.thumb_set pendsv_handler, default_handler
	.weak      systick_handler
	.thumb_set systick_handler, default_handler
	.weak      timer_handler0
	.thumb_set timer_handler0, default_handler
	.weak      timer_handler1
	.thumb_set timer_handler1, default_handler
	.weak      timer_handler2
	.thumb_set timer_handler2, default_handler
	.weak      timer_handler3
	.thumb_set timer_handler3, default_handler
	.weak      pwm_handler
	.thumb_set pwm_handler, default_handler
	.weak      usb_ctrl_handler
	.thumb_set usb_ctrl_handler, default_handler
	.weak      xip_handler
	.thumb_set xip_handler, default_handler
	.weak      pio0_handler0
	.thumb_set pio0_handler0, default_handler
	.weak      pio0_handler1
	.thumb_set pio0_handler1, default_handler
	.weak      pio1_handler0
	.thumb_set pio1_handler0, default_handler
	.weak      pio1_handler1
	.thumb_set pio1_handler1, default_handler
	.weak      dma_handler0
	.thumb_set dma_handler0, default_handler
	.weak      dma_handler1
	.thumb_set dma_handler1, default_handler
	.weak      io_bank0_handler
	.thumb_set io_bank0_handler, default_handler
	.weak      io_qspi_handler
	.thumb_set io_qspi_handler, default_handler
	.weak      sio_proc0_handler
	.thumb_set sio_proc0_handler, default_handler
	.weak      sio_proc1_handler
	.thumb_set sio_proc1_handler, default_handler
	.weak      clocks_handler
	.thumb_set clocks_handler, default_handler
	.weak      spi0_handler
	.thumb_set spi0_handler, default_handler
	.weak      spi1_handler
	.thumb_set spi1_handler, default_handler
	.weak      uart0_handler
	.thumb_set uart0_handler, default_handler
	.weak      uart1_handler
	.thumb_set uart1_handler, default_handler
	.weak      adc_fifo_handler
	.thumb_set adc_fifo_handler, default_handler
	.weak      i2c0_handler
	.thumb_set i2c0_handler, default_handler
	.weak      i2c1_handler
	.thumb_set i2c1_handler, default_handler
	.weak      rtc_handler
	.thumb_set rtc_handler, default_handler
